package czt.ocr.util;

import org.springframework.core.io.ClassPathResource;
import org.springframework.util.FileCopyUtils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class TagFileParser {
    private static final Pattern TAG_PATTERN = Pattern.compile("<(.*?)>");
    private static final Pattern HEADER_PATTERN = Pattern.compile("@\\s*(.*)");
    private static final Pattern DATA_PATTERN = Pattern.compile("#\\s*(.*)");

    public static Map<String, TableData> parseFile(String filePath) throws IOException {
        // 使用 ClassPathResource 获取资源文件
        ClassPathResource resource = new ClassPathResource(filePath);
        try (InputStream inputStream = resource.getInputStream();
             BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8))) {

            Map<String, TableData> result = new HashMap<>();
            TableData currentTable = null;
            String line;

            while ((line = reader.readLine()) != null) {
                line = line.trim();
                if (line.isEmpty()) continue;

                // 检查标签行
                Matcher tagMatcher = TAG_PATTERN.matcher(line);
                if (tagMatcher.find()) {
                    String tagName = tagMatcher.group(1);
                    currentTable = new TableData(tagName);
                    result.put(tagName, currentTable);
                    continue;
                }

                if (currentTable == null) continue;

                // 检查字段行
                Matcher headerMatcher = HEADER_PATTERN.matcher(line);
                if (headerMatcher.find()) {
                    String[] fields = headerMatcher.group(1).split("\\t");
                    for (String field : fields) {
                        currentTable.addField(field.trim());
                    }
                    continue;
                }

                // 检查数据行
                Matcher dataMatcher = DATA_PATTERN.matcher(line);
                if (dataMatcher.find()) {
                    String[] dataParts = dataMatcher.group(1).split("\\t");
                    List<String> row = new ArrayList<>();
                    for (String part : dataParts) {
                        row.add(part.trim());
                    }
                    currentTable.addRow(row);
                }
            }
            return result;
        }
    }

    public static class TableData {
        private final String tableName;
        private final List<String> fields;
        private final List<List<String>> rows;

        public TableData(String tableName) {
            this.tableName = tableName;
            this.fields = new ArrayList<>();
            this.rows = new ArrayList<>();
        }

        public void addField(String field) {
            fields.add(field);
        }

        public void addRow(List<String> row) {
            rows.add(row);
        }

        public String getTableName() {
            return tableName;
        }

        public List<String> getFields() {
            return fields;
        }

        public List<List<String>> getRows() {
            return rows;
        }
    }
}
